package com.eastdigit.servlet;

import com.eastdigit.data.Constant;
import com.eastdigit.util.Encrypt;
import com.eastdigit.util.StringUtil;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ReqBean implements Serializable{
    private String userId;
    // private String userCode;
    private String timestamp;
    private String nonce;
    private String clientDigest;

    private String appKey;
    private String udid;
    private String session;
    private String appVersion;
    private String sysVersion;

    private boolean stateless;

    private String menuId;
    private String url;
    
    private String token;

    public static final String NAME = "reqBean";

    // QueryInfo
    public HashMap<Object, Object> info = new HashMap<Object, Object>();

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }

    public String getSysVersion() {
        return sysVersion;
    }

    public void setSysVersion(String sysVersion) {
        this.sysVersion = sysVersion;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserId() {
        // if (userId == 0) {
        // user = GlobalInput.getCurrentUser();
        // userId = user.getId();
        // }
        return userId;// GlobalInput.getCurrentUserId();
    }

    // public String getUserCode() {
    // return userCode;
    // }
    //
    // public void setUserCode(String userCode) {
    // this.userCode = userCode;
    // }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getNonce() {
        return nonce;
    }

    public void setNonce(String nonce) {
        this.nonce = nonce;
    }

    public String getClientDigest() {
        return clientDigest;
    }

    public void setClientDigest(String clientDigest) {
        this.clientDigest = clientDigest;
    }

    public String getAppKey() {
        return appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getUdid() {
        return udid;
    }

    public void setUdid(String uuid) {
        this.udid = uuid;
    }

    public String getSession() {
        return session;
    }

    public void setSession(String session) {
        this.session = session;
    }

    public void addInfo(Object key, Object value) {
        info.put(key, value);
    }

    public String getInfo(String key) {
        return (String) info.get(key);
    }

    public boolean containsKey(String key) {
        return info != null && info.containsKey(key);
    }

    public Date getInfoDate(String key) {
        Object obj = info.get(key);
        if (obj == null || "".equals(obj.toString())) {
            return null;
        } else if (obj instanceof String) {
            return StringUtil.parse("yyyy-MM-dd", (String) obj);
        }
        return null;
    }

    public int getInfoInt(String key) {
        Object obj = info.get(key);
        if (obj == null || "".equals(obj.toString())) {
            return 0;
        } else if (obj instanceof String) {
            return Integer.parseInt((String) obj);
        }
        return 0;
    }

    public double getInfoDouble(String key) {
        Object obj = info.get(key);
        if (obj == null || "".equals(obj.toString())) {
            return 0.0;
        } else if (obj instanceof String) {
            return Double.parseDouble((String) obj);
        }
        return 0.0;
    }

    public Object getInfoObj(String key) {
        return info.get(key);
    }

    public boolean isStateless() {
        return stateless;
    }

    public void setStateless(boolean stateless) {
        this.stateless = stateless;
    }

    public int getPageIndex() {
        return this.getInfoInt(Constant.PAGE_INDEX);
    }

    public int getPageSize() {
        return this.getInfoInt(Constant.PAGE_SIZE);
    }

    public String toString() {
        if (info == null || info.isEmpty()) {
            return "info is null.";
        }
        String result = "";
        for (Iterator<Map.Entry<Object, Object>> i = info.entrySet().iterator(); i.hasNext();) {
            Map.Entry<Object, Object> entry = i.next();
            result += "[" + entry.getKey() + " - " + entry.getValue() + "]";
        }
        return result;
    }

    public String getMenuId() {
        return menuId;
    }

    public void setMenuId(String menuId) throws Exception {
        if (StringUtil.isNotEmpty(menuId)) {
            this.menuId = Encrypt.decodeAES(menuId);
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}
    
    

}
